#include "apue.h"
#include "error.c"
#include <fcntl.h>
#include <sys/stat.h>

/**
 * Mac OS并不支持futimens函数，没有执行成功
 */
int main(int argc, char const *argv[])
{
    int     i, fd;
    struct stat statbuf;
    struct timespec times[2];

    for(i = 1; i < argc; ++i){
        if(stat(argv[i], &statbuf) < 0){
            err_ret("%s: stat error", argv[i]);
            continue;
        }

        if((fd = open(argv[1], O_TRUNC | O_RDWR)) < 0){
            err_ret("s: open error", argv[1]);
            continue;
        }

        // times[0].tv_sec = statbuf.st_atime; //访问时间
        // times[0].tv_nsec = statbuf.st_atimensec;
        // times[1].tv_sec = statbuf.st_mtime; //内容更新时间
        // times[1].tv_sec = statbuf.st_mtimensec;
        times[0] = statbuf.st_atime; //Mac OS error
        times[1] = statbuf.st_mtime; //Mac OS error
        if(futimens(fd, &times) < 0)
            err_ret("%s: futimes error", argv[1]);
        
        close(fd);
    }
    return 0;
}